{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a3cfc96d",
   "metadata": {},
   "source": [
    "# 1. 人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9888fe86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'request_id': '1679722922,ff95507c-419b-45ce-9f78-305a18090168',\n",
       " 'time_used': 349,\n",
       " 'confidence': 84.397,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975},\n",
       " 'faces1': [{'face_token': '426e7c658c8ccba3aa0d9ffa2f17fc85',\n",
       "   'face_rectangle': {'top': 361, 'left': 257, 'width': 260, 'height': 260}}],\n",
       " 'faces2': [{'face_token': '7a681f98fd9b5bb81527fdcf3993ca9e',\n",
       "   'face_rectangle': {'top': 209, 'left': 228, 'width': 216, 'height': 216}}],\n",
       " 'image_id1': '3ULE2iZe+geUjdpZdIb+Zw==',\n",
       " 'image_id2': 'fmrVOx68bfPBJireREsSzA=='}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('E:\\网络与新媒体\\software\\广州南方学院西3\\lyx.jpg', 'rb')\n",
    "f_02 = open('E:\\网络与新媒体\\software\\广州南方学院西3\\lyx2.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    'api_key':'KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O',\n",
    "    'api_secret':'UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt',\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "         }\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "531f55ab",
   "metadata": {},
   "source": [
    "# 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6add0562",
   "metadata": {},
   "source": [
    "## 2.1 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8430c41",
   "metadata": {},
   "source": [
    "### 步骤：\n",
    "* 1.创建人脸数据集（获得faceset_token，1个人脸库有1个faceset_token，faceset_token相当于库门钥匙）；\n",
    "* 2.检查人脸数据集的情况；\n",
    "* 3.增加人脸信息（增加人脸信息时，faceset_token不变，只需要修改图片的face_token，不同图片的face_token不同）；\n",
    "* 4.增加完毕，可返回再次检查人脸数据集的情况（再次运行2.1.2的代码块，face_tokens即此时人脸数据集里的人脸情况）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5b7a9fe",
   "metadata": {},
   "source": [
    "## 2.1.1 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6604de2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b7364d54cea92d12434913196ae3c61',\n",
       " 'time_used': 167,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1679722980,ddcbe7a8-9f8e-4705-a062-6d1332ee49c6',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'api_key':'KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O',\n",
    "    'api_secret':'UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt',\n",
    "    'display_name':\"广州南方学院西3\"\n",
    "         }\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c306a59",
   "metadata": {},
   "source": [
    "## 2.1.2 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6cbd5c06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b7364d54cea92d12434913196ae3c61',\n",
       " 'tags': '',\n",
       " 'time_used': 110,\n",
       " 'user_data': '',\n",
       " 'display_name': '广州南方学院西3',\n",
       " 'face_tokens': ['426e7c658c8ccba3aa0d9ffa2f17fc85',\n",
       "  '7a681f98fd9b5bb81527fdcf3993ca9e',\n",
       "  'c4089ac4409b382f2b8c4fe57a416eb4'],\n",
       " 'face_count': 3,\n",
       " 'request_id': '1679723271,3f34e471-6c98-4c47-997f-77e66039e03e',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'api_key':'KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O',\n",
    "    'api_secret':'UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt',\n",
    "    'faceset_token':\"6b7364d54cea92d12434913196ae3c61\",\n",
    "\n",
    "         }\n",
    "response = requests.post(request_url, data=params)\n",
    "print(response.status_code)\n",
    "response.json()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f761ea8",
   "metadata": {},
   "source": [
    "## 2.1.3 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "56867f04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679723054,618cf0cb-aaaa-4a73-a75c-297288fabd0f', 'time_used': 111, 'faces': [{'face_token': 'c4089ac4409b382f2b8c4fe57a416eb4', 'face_rectangle': {'top': 153, 'left': 117, 'width': 185, 'height': 185}}], 'image_id': 'sQKAcY+Fg3gsG+GFkJ9lBw==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：刘亦菲、罗云熙\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "f = open('E:\\网络与新媒体\\software\\广州南方学院西3\\mn.jpg','rb')\n",
    "img = base64.b64encode(f.read())\n",
    "params = {\n",
    "    'api_key':'KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O',\n",
    "    'api_secret':'UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt',\n",
    "    'image_base64':img,\n",
    "         }\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7750e876",
   "metadata": {},
   "outputs": [],
   "source": [
    "# face_token：['426e7c658c8ccba3aa0d9ffa2f17fc85','7a681f98fd9b5bb81527fdcf3993ca9e',''c4089ac4409b382f2b8c4fe57a416eb4']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2b3c1bf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b7364d54cea92d12434913196ae3c61',\n",
       " 'time_used': 503,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1679723155,fd4a9c94-37c6-40e3-9884-d7ecbada0642',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O\",\n",
    "    \"api_secret\":\"UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt\",\n",
    "    \"faceset_token\":\"6b7364d54cea92d12434913196ae3c61\",\n",
    "    \"face_tokens\":'426e7c658c8ccba3aa0d9ffa2f17fc85'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "05d39316",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b7364d54cea92d12434913196ae3c61',\n",
       " 'time_used': 451,\n",
       " 'face_count': 2,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1679723180,e8a6e321-1027-4a6e-bd26-21874ae06a40',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O\",\n",
    "    \"api_secret\":\"UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt\",\n",
    "    \"faceset_token\":\"6b7364d54cea92d12434913196ae3c61\",\n",
    "    \"face_tokens\":'7a681f98fd9b5bb81527fdcf3993ca9e'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "75c61fef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b7364d54cea92d12434913196ae3c61',\n",
       " 'time_used': 475,\n",
       " 'face_count': 3,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1679723204,c7a28028-84f7-4505-b689-70b89be73984',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"KKyDfiBX4scl6OfuCr3gnxuMchPKZM0O\",\n",
    "    \"api_secret\":\"UG_2MkCVGI8FiSqXQkDExKXrFztdWgbt\",\n",
    "    \"faceset_token\":\"6b7364d54cea92d12434913196ae3c61\",\n",
    "    \"face_tokens\":'c4089ac4409b382f2b8c4fe57a416eb4'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c35a92a6",
   "metadata": {},
   "source": [
    "## 2.1.4 检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0690bc85",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
